﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKRevitBase.WinForm.HookSuit;
using YJKRevitDimension.DetailIndex.Manager;
using YJKRevitDimension.DetailIndex.Operate;
using YJKRevitDimension.Properties;
using YJKRevitPreview;
using YJKRevitKernel.FormUtility;

namespace YJKRevitDimension.DetailIndex.Forms
{
	public partial class DetailIndexForm : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		public IDetailIndexOperate DetailIndexOperate { get; set; }

		public bool IfChanged { get; set; }

		private void UpdateData()
		{
			this.m_DetailIndexPara.DownScriptText = this.txtDownText.Text;
			this.m_DetailIndexPara.SuperScriptText = this.txtUpText.Text;
			this.m_DetailIndexPara.IndexSymbolDrawingID = this.txtIndexDrawingNo.Text;
			this.m_DetailIndexPara.IndexSymbolID = this.cmbIndexID.Text;
			if (this.ckbFixedAngle.Checked)
			{
				this.m_IsFixedAngle = true;
				try
				{
					this.m_Angle = double.Parse(this.cmbAngle.Text);
					if (this.m_Angle < 0.0 || this.m_Angle > 180.0)
					{
						throw new Exception();
					}
				}
				catch (Exception ex)
				{
					throw ex;
				}
				this.m_DetailIndexPara.IsFixedAngle = true;
				this.m_DetailIndexPara.Angle = this.m_Angle * Math.PI / 180.0;
				return;
			}
			this.m_IsFixedAngle = false;
			this.m_Angle = 0.0;
			this.m_DetailIndexPara.IsFixedAngle = false;
			this.m_DetailIndexPara.Angle = this.m_Angle;
		}

		private void GetDetailIndexType()
		{
			if (this.radDirectIndex.Checked)
			{
				if (this.ckbAndRange.Checked)
				{
					this.m_Type = DetailIndexType.RangeDirectIndex;
				}
				else
				{
					this.m_Type = DetailIndexType.NoRangeDirectIndex;
				}
			}
			else if (this.radSliceIndex.Checked)
			{
				if (this.ckbMultiSlice.Checked)
				{
					this.m_Type = DetailIndexType.MultiSliceIndex;
				}
				else
				{
					this.m_Type = DetailIndexType.SingleSliceIndex;
				}
			}
			else
			{
				this.m_Type = DetailIndexType.NoRangeDirectIndex;
			}
			this.m_DetailIndexPara.Type = this.m_Type;
		}

		public DetailIndexForm(IntPtr iHandle, IDetailIndexOperate OperateIns, ExternalCommandData revit)
		{
			this.InitializeComponent();
			this.cmbIndexID.SelectedIndex = 0;
			this.cmbAngle.SelectedIndex = 0;
			this.cmbAngle.Enabled = false;
			this.IfChanged = false;
			this.m_Revit = revit;
			this.m_iHandle = iHandle;
			this.DetailIndexOperate = OperateIns;
			this.m_DetailIndexPara = new DetailIndexParameter();
			this.m_thisManger = new ControlInfoManager(this, new List<KeyValuePair<System.Windows.Forms.Control, bool>>
			{
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.cmbIndexID, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.txtIndexDrawingNo, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.txtUpText, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.txtDownText, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.radDirectIndex, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.ckbAndRange, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.radSliceIndex, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.ckbMultiSlice, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.ckbFixedAngle, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.cmbAngle, false)
			}, "索引标注", null);
		}

		private void DetailIndexForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.picSimpleImg.Image = YJKRevitDimension.Properties.Resources.NoRange;
			if (!this.m_thisManger.SetControlInfoToForm())
			{
				this.cmbIndexID.SelectedIndex = 1;
				this.txtIndexDrawingNo.Text = "";
				this.txtUpText.Text = "";
				this.txtDownText.Text = "";
				this.radDirectIndex.Checked = true;
			}
			this.TypeChanged();
			this.m_hook = new MouseHook(this);
			this.m_hook.HookStart();
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			base.Close();
			this.SendESC();
		}

		private void TypeChanged()
		{
			this.IfChanged = true;
			try
			{
				this.UpdateData();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			this.GetDetailIndexType();
			switch (this.m_Type)
			{
			case DetailIndexType.NoRangeDirectIndex:
				this.DetailIndexOperate = new NoRangeDetailIndexOperate(this.m_Revit, this.m_DetailIndexPara);
				break;
			case DetailIndexType.RangeDirectIndex:
				this.DetailIndexOperate = new RangeDetailIndexOperate(this.m_Revit, this.m_DetailIndexPara);
				break;
			case DetailIndexType.SingleSliceIndex:
				this.DetailIndexOperate = new SingleSliceDetailIndexOperate(this.m_Revit, this.m_DetailIndexPara);
				break;
			case DetailIndexType.MultiSliceIndex:
				this.DetailIndexOperate = new MultiSliceDetailIndexOperate(this.m_Revit, this.m_DetailIndexPara);
				break;
			}
			this.SendESC();
		}

		private void radDirectIndex_CheckedChanged(object sender, EventArgs e)
		{
			if (!this.radDirectIndex.Checked)
			{
				this.ckbAndRange.Enabled = false;
				this.ckbMultiSlice.Enabled = true;
				return;
			}
			this.ckbAndRange.Checked = false;
			this.ckbAndRange.Enabled = true;
			this.ckbMultiSlice.Enabled = false;
			if (this.ckbAndRange.Checked)
			{
				this.picSimpleImg.Image = YJKRevitDimension.Properties.Resources.Range;
				return;
			}
			this.picSimpleImg.Image = YJKRevitDimension.Properties.Resources.NoRange;
		}

		private void ckbAndRange_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radDirectIndex.Checked)
			{
				if (this.ckbAndRange.Checked)
				{
					this.picSimpleImg.Image = YJKRevitDimension.Properties.Resources.Range;
					return;
				}
				this.picSimpleImg.Image = YJKRevitDimension.Properties.Resources.NoRange;
			}
		}

		private void radSliceIndex_CheckedChanged(object sender, EventArgs e)
		{
			if (!this.radSliceIndex.Checked)
			{
				this.ckbMultiSlice.Enabled = false;
				this.ckbAndRange.Enabled = true;
				return;
			}
			this.ckbMultiSlice.Checked = false;
			this.ckbMultiSlice.Enabled = true;
			this.ckbAndRange.Enabled = false;
			if (this.ckbMultiSlice.Checked)
			{
				this.picSimpleImg.Image = YJKRevitDimension.Properties.Resources.MultipleSliece;
				return;
			}
			this.picSimpleImg.Image = YJKRevitDimension.Properties.Resources.SingleSliece;
		}

		private void ckbMultiSlice_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radSliceIndex.Checked)
			{
				if (this.ckbMultiSlice.Checked)
				{
					this.picSimpleImg.Image = YJKRevitDimension.Properties.Resources.MultipleSliece;
					return;
				}
				this.picSimpleImg.Image = YJKRevitDimension.Properties.Resources.SingleSliece;
			}
		}

		private void SendESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			DetailIndexForm.SendMessage(this.m_iHandle, wMsg, IntPtr.Zero, IntPtr.Zero);
			DetailIndexForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			DetailIndexForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void DetailIndexForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.m_thisManger.GetControlInfoToXml();
			if (this.m_hook != null)
			{
				this.m_hook.HookStop();
			}
			this.SendESC();
		}

		private void ckbFixedAngle_CheckedChanged(object sender, EventArgs e)
		{
			if (this.ckbFixedAngle.Checked)
			{
				this.cmbAngle.Enabled = true;
				return;
			}
			this.cmbAngle.Enabled = false;
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg == 1125)
			{
				this.ChangeOperater();
				return;
			}
			base.DefWndProc(ref m);
		}

		private void ChangeOperater()
		{
			try
			{
				this.TypeChanged();
			}
			catch (Exception)
			{
				YJKMessageBox.Show("固定角度处只能输入0到180度之间的数字，且不支持输入非数字字符串");
				base.Focus();
			}
		}

		private MouseHook m_hook;

		private Previewer m_Previewer;

		public string m_DownScriptText;

		public string m_SuperScriptText;

		public string m_IndexSymbolDrawingID;

		public string m_IndexSymbolID;

		public DetailIndexType m_Type;

		public bool m_IsFixedAngle;

		public double m_Angle;

		public DetailIndexParameter m_DetailIndexPara;

		private ExternalCommandData m_Revit;

		private IntPtr m_iHandle;

		private ControlInfoManager m_thisManger;
	}
}
